<template>
  <div>
    <el-dialog
      v-if="dialog"
      v-dialogDrag="dialog"
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="dialog"
      width="40%"
    >
      <el-form
        ref="actionForm"
        :model="actionForm"
        :rules="actionRules"
        label-suffix=" :"
        label-width="100px"
      >
        <el-form-item label="值集代码" prop="valueSetCode">
          <el-input
            v-model.trim="actionForm.valueSetCode"
            :disabled="title == '修改值集代码'"
          />
        </el-form-item>
        <el-form-item label="值集描述" prop="valueDesc">
          <el-input v-model.trim="actionForm.valueDesc" type="textarea" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog = false"> 取 消 </el-button>
        <el-button
          v-oauth data-oauthUuid="1620437844000"
          type="primary"
          :loading="loading"
          @click="onSubmit('actionForm')"
        >
          {{ loading ? "提交中 ..." : "确定" }}
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { saveValueSetCode, updateValueSetCode } from "@/api/valueset";
import { deepClone } from "@/utils/index";

export default {
  name: "Valuecode",
  data() {
    return {
      title: "新增值集代码",
      loading: false,
      dialog: false,
      actionRules: {
        valueSetCode: [
          { required: true, message: "请输入值集代码", trigger: "blur" },
        ],
      },
      actionForm: {
        valueSetCode: "",
        valueDesc: "",
      },
    };
  },
  methods: {
    show(data) {
      if (data) {
        this.title = "修改值集代码";
        this.actionForm = deepClone(data);
      } else {
        this.title = "新增值集代码";
        this.actionForm = {
          valueSetCode: "",
          valueDesc: "",
        };
      }
      this.dialog = true;
    },
    close() {
      this.dialog = false;
      this.$emit("refresh");
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.title == "新增值集代码") {
            this.docreat();
          } else {
            this.domodify();
          }
        } else {
          return false;
        }
      });
    },
    docreat() {
      saveValueSetCode(null, {
        valueSetCode: this.actionForm.valueSetCode,
        valueDesc: this.actionForm.valueDesc,
      })
        .then(() => {
          this.close();
          this.$message.success("添加成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    domodify() {
      updateValueSetCode(null, {
        id: this.actionForm.id,
        valueDesc: this.actionForm.valueDesc,
        valueSetCode: this.actionForm.valueSetCode,
      })
        .then(() => {
          this.$message.success("修改成功");
          this.close();
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
